import * as echarts from '../../../../ec-canvas/echarts';
import { getStateTime } from '../../../../api/getData.js'
import { formatTime } from '../../../../utils/util.js'
const systemInfo = wx.getSystemInfoSync();
const App = getApp();
Component({
  properties: {
  
  },
  data: {
    ec: {
      lazyLoad: true
    },
    startTime: '',
    endTime: '',
    date: ''
  },
  ready() {
    const date = this.countDate();
    let nowDate = formatTime(new Date(), 1, true)
    this.setData({
      startTime: date,
      endTime: nowDate
    });
    this.getCateOption();
  },
  pageLifetimes: {
  },
  methods: {

    // 配置折线图
    getCateOption() {
      let { startTime, endTime } = this.data;
      startTime = startTime.replace(/-/g, '/') + ' ' + '00:00:00'
      endTime = endTime.replace(/-/g, '/') + ' ' + '23:59:59'
      let halfYear = 1000 * 60 * 60 * 24 * 30 * 6
      if ((new Date(endTime).getTime() - new Date(startTime).getTime() > halfYear) ){
        wx:wx.showToast({
          title: '选择时间区间不能超过半年',
          icon: 'none',
        })
      } else {
        getStateTime(new Date(startTime).getTime(), new Date(endTime).getTime()).then(res => {
          if (res.resCode === "SUCCESS") {
            let serriesArr = [];
            Object.keys(res.result.runtime).map((i) => {
              serriesArr.push({
                value: res.result.runtime[i] / 3600,
              })
            })
            let mychart = this.selectComponent("#mychart-dom-time");
            mychart.init((canvas, width, height) => {
              const Chart = echarts.init(canvas, null, {
                width: width,
                height: height
              });
              Chart.setOption({
                tooltip: {
                  triggerOn: 'click',
                  trigger: 'axis',
                  confine: true,
                  extraCssText: "z-index:98",
                  formatter: function (params) {
                    var result = '时间(h)';
                    params.forEach(function (item) {
                      result += "\n" + item.name + ': ' + item.value.toFixed(2);
                    });
                    return result;
                  }
                },
                xAxis: {
                  type: 'value',
                  axisLabel: {
                    show: true,
                    textStyle: {
                      color: '#9095A5',
                      fontSize: '14'
                    }
                  },
                  axisLine: {
                    show: true,
                    lineStyle: {
                      color: '#9095A5'
                    }
                  },
                  splitLine: {
                    show: true,
                    lineStyle: {
                      color: '#6B6E7B'
                    }
                  },
                  boundaryGap: [0, 0.01]
                },
                yAxis: {
                  type: 'category',
                  axisLabel: {
                    show: true,
                    textStyle: {
                      color: '#9095A5',
                      fontSize: '13'
                    }
                  },
                  axisLine: {
                    show: true,
                    lineStyle: {
                      color: '#9095A5'
                    }
                  },
                  data: Object.keys(res.result.runtime)
                },
                series: [
                  {
                    name: '2011年',
                    type: 'bar',
                    barWidth: 40,
                    animation: true,
                    itemStyle: {
                      normal: {
                        color: function (params) {
                          const colorList = { '停机': '#FF5F5F', '运转': '#3AD5AB', '卸载': '#859bf4', '异常': '#ffd285', '放电': '#409EFF', '放电干扰': '#909399', '过渡': '#cddc39', '清理': '#63e0ff' }
                          return colorList[params.name]
                        },
                      }
                    },
                    barWidth: 10,
                    data: serriesArr
                  },
                ]
              })
              return Chart
            })
          }
        })
      }
      
    },
    bindStartTimeChange(e) {
      this.setData({
        startTime: e.detail.value
      })
      // this.getCateOption()
    },
    bindEndTimeChange(e) {
      this.setData({
        endTime: e.detail.value
      })
      this.getCateOption()
    },
    // 算本周一日期
    countDate () {
      let nowTemp = new Date();//当前时间

      let oneDayLong = 24 * 60 * 60 * 1000;//一天的毫秒数

      let c_time = nowTemp.getTime();//当前时间的毫秒时间

      let c_day = nowTemp.getDay() || 7;//当前时间的星期几

      let m_time = c_time - (c_day - 1) * oneDayLong;//当前周一的毫秒时间

      let monday = new Date(m_time);//设置周一时间对象

      let m_year = monday.getFullYear();

      let m_month =('0' + (monday.getMonth() + 1)).slice(-2);

      let m_date =('0' + monday.getDate()).slice(-2);
      return `${m_year}-${m_month}-${m_date}`
    }
  }
})